#include "search.h"

#include <QFont>
#include <QPoint>
#include <QPen>
#include <QBrush>
#include <QPushButton>
#include <QTimer>


#include <QByteArray>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QUrlQuery>

search::search()
{

}
search::search(QWidget *parent)
    : QWidget(parent)
{
    color = QColor (204, 235, 255);
    //初始化映射表
    initializeCityCodes();
    //连接信号
    network_1 = new weather_json(this);
    connect(network_1, &weather_json::weatherDataUpdated,this, &search::onWeatherDataUpdated);
    // 创建水平布局
    QHBoxLayout *layout = new QHBoxLayout(this);

    // 创建搜索框
    searchEdit = new QLineEdit(this);
    searchEdit->setPlaceholderText("请输入城市名称...");
    searchEdit->setClearButtonEnabled(true);

    // 创建标题标签
    titleLabel = new QLabel("天气预报", this);
    titleLabel->setAlignment(Qt::AlignCenter);

    // 设置大小策略，使其能够扩展以填充可用空间
    searchEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // 设置字体和样式，确保在小空间内可读
    QFont font = searchEdit->font();
    font.setPointSize(10);
    searchEdit->setFont(font);
    titleLabel->setFont(font);

    // 精简样式，减少内边距
    searchEdit->setStyleSheet(
        "QLineEdit {"
        "    padding: 5px;"
        "    border: 1px solid #ccc;"
        "    border-radius: 4px;"
        "    font-size: 10px;"
        "}"
    );

    titleLabel->setStyleSheet(
        "QLabel {"
        "    background-color: #1890ff;"
        "    color: white;"
        "    padding: 5px 10px;"
        "    border-radius: 4px;"
        "    font-size: 10px;"
        "    font-weight: bold;"
        "}"
    );

    // 添加到布局
    layout->addWidget(searchEdit, 3);
    layout->addWidget(titleLabel, 1);

    // 减少布局边距和间距
    layout->setContentsMargins(2, 2, 2, 2);
    layout->setSpacing(5);


    connect(searchEdit, &QLineEdit::textChanged, this, &search::onTextChanged);
    connect(searchEdit, &QLineEdit::textChanged, this, [this](const QString &text){
        // 添加防抖，避免频繁请求
        static QTimer timer;
        timer.setSingleShot(true);
        timer.start(500); // 500ms后执行

        connect(&timer, &QTimer::timeout, this, [this, text](){
            // 验证对象是否仍然有效
            if (!text.isEmpty()) {
                QUrl url("http://t.weather.itboy.net/api/weather/city/"+to_int(searchEdit->text()));
                qDebug()<<url;
                network_1->mNetAccessManager->get(QNetworkRequest(url));
            }
        }, Qt::UniqueConnection); // 确保只有一个连接
    });

}

void search::onTextChanged(const QString &text)
{
    if (!text.isEmpty()) {
        titleLabel->setText(text);
    } else {

    }
}
void search::onWeatherDataUpdated(const QVector<WeatherData> &weatherData)
{
    // 转发信号
    emit weatherDataUpdated(weatherData);
}
void search::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    // 创建 QPainter 对象
    QPainter painter(this);

    // 启用抗锯齿效果，使绘制更加平滑
    painter.setRenderHint(QPainter::Antialiasing);

    // 设置画笔颜色和宽度
    QPen pen;
    pen.setColor(this->color);  // 设置边框颜色为黑色
    pen.setWidth(0);  // 设置边框宽度
    painter.setPen(pen);

    // 设置画刷颜色
    QBrush brush;
    brush.setColor(this->color);  // 设置填充颜色为浅蓝色
    brush.setStyle(Qt::SolidPattern);
    painter.setBrush(brush);

    // 获取控件的矩形区域
    QRect rect = this->rect();

    // 绘制圆角矩形，rect 是控件的区域，设置圆角半径为 15
    painter.drawRoundedRect(rect, 15, 15);
}
